// pages/mySelf/mySelf.js
const app = getApp();


Page({

  /**
   * 页面的初始数据
   */
  data: {
     loginUser:app.cache.get('loginUser'),
     toUserId:''
  },

  toProfessRecordListFrom(){
    swan.navigateTo({
        url: '/packageC/pages/professId/professId'
    });
  },
  toProfessRecordListTo(){
    swan.navigateTo({
        url: '/packageC/pages/professIdTo/professIdTo'
    });
  },
  addFriends() {
    swan.navigateTo({
        url: '/packageC/pages/addFriends/addFriends'
    })  
  },
//   onlineService() {
//     swan.navigateTo({
//         url: '/packageC/pages/addFriends/addFriends'
//     })  
//   },

  getToUserId(e){
     this.setData({
         toUserId:e.detail.value
     })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },


  getPhoneNumber(e) {
    console.log(e);
    let that_detial = e.detail;
    if(that_detial.encryptedData != null) {
        swan.login({
            success: res => {
                // console.log('login success', res);
                let code = res.code;
                let url = app.globalData.URL + `/8200/auth/login/${code}`;
                swan.request({
                    url: url,
                    method:'get',
                    success:res=>{
                        // console.log("login request",res)

                        if(res.data.code != 200){
                            app.showToastFail(res.data.msg)
                            return;
                        }

                        app.cache.put("Authorization",res.data.data.authorization,24*60*60)

                        swan.request({

                            url:app.globalData.URL + `/8100/user/findOneById/${res.data.data.id}`,
                            method: 'get',
                            success:re=>{

                                if(re.data.code != 200){
                                    app.showToastFail(re.data.msg)
                                    return;
                                }
                                // console.log("findOneById ok : loginUser",re.data.data)

                                // aprisKeyWords转对象
                                if(re.data.data.aprisKeyWords == null || re.data.data.aprisKeyWords == ''){
                                    re.data.data.aprisKeyWords = ['','','']
                                }else{
                                    re.data.data.aprisKeyWords = JSON.parse(re.data.data.aprisKeyWords)
                                }

                                app.cache.put('loginUser',re.data.data,24*60*60)

                                this.setData({
                                    loginUser:app.cache.get('loginUser')
                                })

                                // console.log("user phone ：",re.data.data.phone)
                                app.showToastSuccess('登录成功！')


                                if(re.data.data.phone != '' && re.data.data.phone != null && re.data.data.phone != undefined){
                                    return;
                                }

                                this.updatePhone(that_detial);
                            }
                        })


                    },
                    fail:err=>{
                        // console.log(err)
                        app.showToastFail('登录失败！')
                    }
                })

            },
            fail: err => {
                // console.log('login fail', err);
                app.showToastFail('登录失败！')
            }
        });
    }

    

  },

  updatePhone(res){


    swan.request({
        url: app.globalData.URL + "/8100/user/getPhoneNumber",
        method:'post',
        header:{
            'Content-Type':'application/json',
            'Authorization':app.cache.get('Authorization')
        },
        data:res,
        success:r=>{
            // console.log("getPhoneNumber...")
            // console.log(r);
            //更新缓存
            // aprisKeyWords转对象
            if(r.data.data.aprisKeyWords == null || r.data.data.aprisKeyWords == ''){
                r.data.data.aprisKeyWords = ['','','']
            }else{
                r.data.data.aprisKeyWords = JSON.parse(r.data.data.aprisKeyWords)
            }
            app.cache.put('loginUser',r.data.data,24*60*60);

            this.setData({
                loginUser:app.cache.get('loginUser')
            })
        },
        fail:err=>{
            console.log(err)
        }
    })

  },


  updateuserInfo(that_detail){

    let data = app.cache.get('loginUser')
    data.nickName = that_detail.userInfo.nickName
    data.userPic = that_detail.userInfo.avatarUrl
    data.sex = that_detail.userInfo.gender
    data.aprisKeyWords = JSON.stringify(data.aprisKeyWords);

    let url = app.globalData.URL + `/8100/user/update`;

    swan.request({
        url: url,
        method:'post',
        data:data,
        success:res=>{
            console.log(res);

            if(res.data.code != 200){
                app.showToastFail(res.data.msg)
                return;
            }

            this.setData({
                loginUser:res.data.data
            })

            console.log(this.data.loginUser);
            if(res.data.data.aprisKeyWords == null || res.data.data.aprisKeyWords==''){
                this.setData({
                    'loginUser.aprisKeyWords':['','',''],
                })
            }else{
                this.setData({
                    'loginUser.aprisKeyWords':JSON.parse(res.data.data.aprisKeyWords)
                })
            }
            app.cache.put("loginUser",this.data.loginUser,24*60*60);
            app.showToastSuccess('成功获取用户信息')
        },
        fail:err=>{
            console.log(err)
            app.showToastFail('获取用户信息失败')
        }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    let user = app.cache.get('loginUser');

    if(user == undefined || user == ''){
        return
    }

    this.setData({
        loginUser:user
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  },
})